package com.example.springbootdemo.api.controller;

import com.example.springbootdemo.api.DTO.AddStudentDTO;
import com.example.springbootdemo.domin.VO.getScoreDetailVO;
import com.example.springbootdemo.domin.entity.Student;
import com.example.springbootdemo.infra.util.Result;
import com.example.springbootdemo.app.service.StudentService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/student")
@Api(value = "学生相关controller", tags = {"学生相关接口"})
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;
    private final Logger logger = LoggerFactory.getLogger(StudentController.class);

    /**
     * 根据id查询学生信息
     *
     * @param sno
     * @return
     */
    @GetMapping("/getById/{sno}")
    public ModelAndView getById(@PathVariable Integer sno) {
        if (sno == null) {
            //throw new Exception();todo
        }
        logger.info("查询的学生id为:{}", sno);
        Student student = studentService.getById(sno);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("change_student");
        modelAndView.addObject("student", student);
        return modelAndView;
    }

    /**
     * 删除学生信息
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteById/{sno}")
    public ModelAndView deleteById(@PathVariable("sno") Integer id) {
        if (id == null) {
            //throw new Exception();todo
        }
        studentService.deleteById(id);
        List<Student> all = studentService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("student");
        modelAndView.addObject("st", all);
        return modelAndView;
    }

    /**
     * 添加单个学生
     *
     * @param student
     * @return
     */
    @PostMapping("/addStudent")
    public ModelAndView addStudent(Student student) {
        studentService.insertStudent(student);
        List<Student> all = studentService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("student");
        modelAndView.addObject("st", all);
        return modelAndView;
    }

    /**
     * 添加多个学生,批量插入
     */
    @PostMapping("/addStudents")
    public Result addStudents(@RequestBody List<AddStudentDTO> list) {
        studentService.insertStudents(list);
        return Result.success();
    }

    @GetMapping("/getAll")
    public ModelAndView getAll() {
        List<Student> all = studentService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("student");
        modelAndView.addObject("st", all);
        return modelAndView;
    }

    @GetMapping("/updateById/{sno}")
    public ModelAndView updateById(@PathVariable("sno") Integer sno) {
        if (sno == null) {
            //throw new Exception();todo
        }
        Student student = studentService.getById(sno);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("change_student");
        modelAndView.addObject("student", student);
        return modelAndView;
    }

    /**
     * 更新学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/update")
    public ModelAndView update(Student student) {
        log.info("输出对象信息:{}", student);
        studentService.update(student);
        List<Student> all = studentService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("student");
        modelAndView.addObject("st", all);
        return modelAndView;
    }

    /**
     * 查询学生的成绩
     */
    @GetMapping("/getScoreDetail/{sname}")
    public Result<List<getScoreDetailVO>> getScore(@PathVariable("sname") String sname) {
        List<getScoreDetailVO> scoreDetail = studentService.getScoreDetail(sname);
        return Result.success(scoreDetail);
    }
}
